//
//  UIImage+Additions.m
//  Objective-C
//
//  Created by 陆承东 on 2018/5/4.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "UIImage+SLCategory.h"

#import <objc/runtime.h>

@implementation UIImage (SLCategory)

+ (void)load {
    Method originalMetnod = class_getClassMethod([UIImage class], @selector(imageNamed:));
    Method currentMethod = class_getClassMethod([UIImage class], @selector(sl_imageNamed:));
    method_exchangeImplementations(originalMetnod, currentMethod);
}

+ (instancetype)sl_imageNamed:(NSString *)name {
    UIImage *image = [UIImage sl_imageNamed:name];
    
    if (image) {
#if DEBUG
        NSLog(@"\n==========> \nFILE:%s\nFUNCTION:%s\n加载成功 \n<==========\n", __FILE__, __FUNCTION__);
#endif
    } else {
#if DEBUG
        NSLog(@"\n==========> \nFILE:%s\nFUNCTION:%s\n加载失败 \n<==========\n", __FILE__, __FUNCTION__);
#endif
    }
    
    return image;
}

#pragma mark ----- 根据颜色生成图片
+ (UIImage *)imageWithColor:(UIColor *)color {
    CGSize size = CGSizeMake(1, 1);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
    UIImage * desImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return desImage;
}

+ (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

/**
 *  获取矩形的渐变色的UIImage(此函数还不够完善)
 *
 *  @param bounds       UIImage的bounds
 *  @param colors       渐变色数组，可以设置两种颜色
 *  @param gradientType 渐变的方式：0--->从上到下   1--->从左到右
 *
 *  @return 渐变色的UIImage
 */
- (UIImage*)gradientImageWithBounds:(CGRect)bounds andColors:(NSArray*)colors andGradientType:(int)gradientType {
    NSMutableArray *ar = [NSMutableArray array];
    
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    UIGraphicsBeginImageContextWithOptions(bounds.size, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
    CGPoint start = CGPointMake(0.0, 0.0);;
    CGPoint end;
    
    switch (gradientType) {
        case 0:
            end = CGPointMake(0.0, bounds.size.height);
            break;
        case 1:
            end = CGPointMake(bounds.size.width, 0.0);
            break;
        default:
            end = CGPointMake(0.0, 0.0);
    }
    CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    return image;
}

@end
